<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Luckydog
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('luckydog/User_model', 'user');
        $login_status = $this->login_status = $this->check_login_status();
        if (empty($login_status)){
            redirect('/luckydog/login');
        }
    }



    public function index()
    {
        $_SESSION['v_menu'] = 'user';
        $starttime = $this->input->get("starttime",true)?$this->input->get("starttime",true):"";
        $endtime = $this->input->get("endtime",true)?$this->input->get("endtime",true):"";
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $contact = $this->input->get('contact') ? $this->input->get('contact') : "";
        $name = $this->input->get('name') ? $this->input->get('name') : "";
        $filter = array(
            'starttime'=>strtotime($starttime),
            'endtime'=>strtotime($endtime),
            'contact'=>($contact),
            'name'=>$name,
            'sign'=>$sign
        );
        $limit = 20;
        $total_rows = $this->user->get_user_cnt($filter);
        $base_url = '/luckydog/user/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $userList = $this->user->userList($filter,$per_page,$limit);

        
        $data = array(
            'userList'=>$userList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'total_rows'=>$total_rows,
        );
        $this->load->view('/luckydog/user/user_list',$data);
    }  

    public function  textures()
    {
        $departmentList = $this->user->getDepartentList();
        $data = ['departmentList'=>$departmentList];
        $this->load->view("/luckydog/user/textures", $data);
    }
    public function editUser(){
        $AccountID = $this->input->post("AccountID",true) ? $this->input->post("AccountID",true) : 0;
        $username = $this->input->post("username",true) ? $this->input->post("username",true) : 0;
        $password = $this->input->post("password",true) ? $this->input->post("password",true) : 0;
        $departmentid = $this->input->post("departmentid",true) ? $this->input->post("departmentid",true) : 0;
        $truename = $this->input->post("truename",true) ? $this->input->post("truename",true) : 0;
        $data = [];
        $data['username']       =   $username;
        $data['password']       =   md6($password);
        $data['departmentid']   =   $departmentid;
        $data['truename']       =   $truename;
        if(empty($AccountID)){
            $data['addtime']    =time();    
        }
        $res = $this->user->editUser($AccountID, $data);
        echo $res;
    }
    /**
     *------------------------------------------------------------------------------------------------------------------
     * @title：编辑用户停用启用
     * @param：id      
     * @author:：柏凯
     * @date：2015-09-22
     *------------------------------------------------------------------------------------------------------------------
     */
    public function editStatus(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $status = $this->input->post("status",true) ? $this->input->post("status",true) : "";
        $data = array(
            "id"=>$id,
            "Status"=>$status
        );
        //$res =$this->user->editStatus("bp_user",$data);
        //echo $res;
    }

    public function import(){
        $_SESSION['v_menu'] = 'user';
        $this->load->view("/luckydog/import/userimport");
    }
    public function fenxi()
    {
        if(empty($_FILES)) header("Location:index");
        require_once(APPPATH.'libraries/PHPExcel.php');
        require_once(APPPATH.'libraries/PHPExcel/IOFactory.php');
        require_once(APPPATH.'libraries/PHPExcel/Reader/Excel5.php');
        
        $showmessage = '';
        $tmp_name = $_FILES["fileData"]['tmp_name'];
        $name = $_FILES["fileData"]['name'];
        $filenamesuffix = substr($name,stripos($name,".")+1);
        if(in_array($filenamesuffix,['xls','xlsx'])){
            $result = $this->analyseExcel($tmp_name);
            $showmessage .= $_FILES["fileData"]['name']."<br />".$result;
        }else{
            $showmessage .= "文件格式错误";
        }
        $data = ['message'=>$showmessage];
        $this->load->view("/luckydog/import/userimport",$data);
    }
    /**
     * 分析excel数据
     * @return [type] [description]
     */
    public function analyseExcel($tmp_name){
        $objReader = PHPExcel_IOFactory::createReader('Excel2007');//use Excel2007 for 2007 format 
        $objPHPExcel = $objReader->load($tmp_name); //$filename可以是上传的文件，或者是指定的文件
        $sheet = $objPHPExcel->getSheet(0); 
        $highestRow = $sheet->getHighestRow(); // 取得总行数 
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        if(!$highestRow || !$highestColumn){
            return false;
        }
        // 根据自己的数据表的大小修改
         $arr = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I',10=>'J',11=>'K',12=>'L',13=>'M', 14=>'N',15=>'O',16=>'P',17=>'Q',18=>'R',19=>'S',20=>'T',21=>'U',22=>'V',23=>'W',24=>'X',25=>'Y',26=>'Z');
         
         // 每次读取一行，再在行中循环每列的数值
         for ($row = 1; $row <= $highestRow; $row++) {
            for ($column = 0; $arr[$column] != 'I'; $column++) {
                $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
                $list[$row][] = nl2br($val);
            }
         }
         //echo "<pre>".var_dump(($list))."</pre>";
         $paperID = $departmentID = 0;
         if(empty($list)) return false;
         $addtime = time();
         $showmessage = "";
         foreach ($list as $key => $value) {
             if($key >= 3)
             {
                $usersData = [
                                'truename'=>$value[0],
                                'username'=>$value[1],
                                'password'=>md6($value[2]?$value[2]:"123456"),
                                'departmentid'=>$value[3],
                                'sex'=>$value[4],
                                'zhuanye'=>$value[5],
                                'zhicheng'=>$value[6],
                                'qq'=>$value[7],
                                'mobile'=>$value[8],
                                'addtime'=>$addtime,
                                'operationname'=>$_SESSION['adminUserInfo']['id'],
                                ];
                $result = $this->user->editUser(0, $usersData);
                if($result)
                {
                    $showmessage .= "<span style='color:green;'>账号‘".$value[1]."’成功</span><br />";
                }else{
                    $showmessage .= "<span style='color:red;'>账号‘".$value[1]."’已存在</span><br />";
                }
             }
         }
         return $showmessage;
    }


}
/* End of file user.php */
/* Location: ./application/controllers/luckydog/user.php */
